Skip to content

MappedQuery dedupe, and a public QueryOptions with WithOptions to set it - #289

Merged
electricessence merged 6 commits into
masterfrom
refactor/mappedquery-dedupe
Dec 2, 2026
Merged

electricessence merged 6 commits into
masterfrom
refactor/mappedquery-dedupe

Conversation

@electricessence

@electricessence electricessence commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Removes repeated code in the mapped query family, and makes the four switches of a mapped query one public value with one call to set them. The refactor changes no behavior and nothing existing in the public API (the public member list and its order are the same; there is no benchmark because nothing here is on a per-call path: these are the query-build steps). The one addition to the public API is QueryOptions and WithOptions (below).

What changed

  • One options value. The four switches of a mapped query (SingleResult, AsyncReads, CancelOnEarlyStop, SequentialAccess) were four bool fields, four constructor parameters and nine hand-spelled copies. They are now one [Flags] enum QueryOptions : byte (AsyncReads 1, SingleResult 2, CancelOnEarlyStop 4, SequentialAccess 8, the bits the query's hash code has always mixed in).
    • The query core takes it in place of four bools (seven constructor arguments, not ten). One core method sets all four; the named switches add their one flag to the current value.
    • Equality is == and the hash part is (int)options. A switch is read with a mask ((options & QueryOptions.X) != 0), never HasFlag, which boxes on the older targets. The terminals still pass bools into their state, at no cost per row.
    • MappedDbQuery holds the value in place of four bool fields (five constructor arguments, not eight). View() makes the core with its options in one call instead of re-applying four switches one by one, then the pool.
  • One default source. Views.DefaultSource<T>() is the "first column for a single value, the shared default map otherwise" choice of a plain Map<T>(), which was written seven times (DbQuery, DbQueryCall, DbQueryUsing and the four view factories). At the four extension sites the old fallback was the RowMap overload, which for a non-null map is exactly the matching core factory, so they now call it directly.
  • Forwarding on the bound query. DbQueryUsing.Map<T>(), Map(RowMap), Map(Func), AsObjectArrays() and AsDictionaryRows() repeated the bodies of DbQueryCall; they now forward to the call they hold, as its AnyAsync and CountAsync already did. Map(Func) keeps its own selector check first, so a default value with no selector still reports the selector, as it always did.
  • One wording for the docs of the members it touches. The five DbQueryCall members above hold the text, built from the shared members of Mapped.docs.xml (row-type, map-overload, and two new ones: default-bound, the "default value" exception, and returns-mapped-query). The same five members of DbQueryUsing inherit it instead of keeping their own copies. The selector parameter keeps its "called from any thread and by overlapping calls" text. Rendered in DocFX with no warnings, each of the five keeps its summary, remarks, type parameter, parameter, returns and exceptions.
  • Line width. Lines this change adds or touches stay within about 80 columns: an expression body's => goes on its own line, and long argument lists break one per line.

Not in this PR: folding the bodies of the First/Single one-row terminals into FirstOrDefault/SingleOrDefault. That is on the per-call path and waits for its benchmark, which was added separately.

The public addition: QueryOptions and WithOptions

  • QueryOptions is a public [Flags] enum (None, AsyncReads 1, SingleResult 2, CancelOnEarlyStop 4, SequentialAccess 8): how a mapped query executes its command and reads its rows. The summary of each value is inherited from the method of the same name.
  • WithOptions(QueryOptions options) on MappedQuery<T>, MappedQueryWithSkip<T>, MappedQueryWithTake<T>, MappedCommand<T>, MappedDbQuery<T> and MappedDbQuery<TParams, T> returns a query whose switches are exactly options.
    • It replaces; it does not combine. Any switch not in the value is turned off, including one set earlier in the chain or on the command before Map, so WithOptions(QueryOptions.None) clears all four and is the only way to turn a switch off.
    • To add one switch, the named method is the call; each of them points to WithOptions in its docs. Skip and take are kept.
  • A value with a bit that is none of the switches throws ArgumentOutOfRangeException (parameter options) whose message names the valid values, before a default value is looked at.
  • There is no WithOptions on MappedReader<T> or MappedTable<T>: a Map over a reader or a table you already have has no command to execute.
  • PublicAPI.Unshipped.txt lists the enum, its values and the six methods; the release notes have a bullet under "Changes since rc.16".

Why there is no behavior change

  • QueryOptionsTests pin what the refactor must keep: each switch makes a query that no other switch stands for, any order of the switches is the same query, a switch set twice is the same as once, and a definition's switches and pool reach the query made from it. They pass on the code before this change as well (checked against the previous master).
  • OptionsSetterTests pin the setter: for all 16 combinations WithOptions(x) is the same query (equal, same hash) as the chained calls, on a command view, a mapped query, a skipped and a taken query, and a definition (typed or not). It replaces earlier switches, and WithOptions(None) turns off what a command chose before Map. It keeps skip and take. A bit that is no switch is refused on all six types, before a default value is looked at.
  • The existing tests pass unchanged, with the generator on and off.
  • Reviewed by a small reviewer swarm, arbitrated: no defects found. Points the reviewers could not verify from the diff alone were checked by reading the code.

Checks

  • Core tests: 15,759 pass in Release with the generator on; 15,738 pass (16 skipped) in Debug with it off.
  • 231 sample checks pass; the Visual Studio solution build has no errors on any target framework; dotnet format style and analyzers at --severity info report nothing on all 14 projects; the docs build is clean.

🤖 Generated with Claude Code

electricessence and others added 5 commits December 1, 2026 20:49
… on the bound query

No behavior change and no public API change (the public member list and order are the same).

- The four switches of a mapped query (`SingleResult`, `AsyncReads`, `CancelOnEarlyStop`, `SequentialAccess`) are one internal readonly struct, `QueryOptions` (a plain struct with `With*` copies, as the netstandard targets have no `init` accessors). The query core takes it in place of four bools (seven constructor arguments, not ten), each `With*` is one copy call, and its equality compares the value; the hash code mixes the same bits as before. `MappedDbQuery` holds the value in place of four bool fields (five constructor arguments, not eight), its setters are copies of it, and `View()` makes the core with its options in one call instead of re-applying four, then the pool.
- `Views.DefaultSource<T>()` is the first-column-or-default-map choice of a plain `Map<T>()`, written seven times before (`DbQuery`, `DbQueryCall`, `DbQueryUsing`, and the four view factories).
- `DbQueryUsing.Map<T>()`, `Map(RowMap)`, `Map(Func)`, `AsObjectArrays()` and `AsDictionaryRows()` forward to the call they hold (`ToCall().Map<T>()` and so on), as its `AnyAsync` and `CountAsync` already did, instead of repeating the call's bodies. `Map(Func)` keeps its own selector check first, so a default value with no selector reports the selector, as it always did.

Tests: `QueryOptionsTests` pins what the refactor must keep: each switch makes another query that no other switch stands for, any order of the switches is the same query, a switch set twice is the same as once, and a definition's switches and pool reach the query made from it. They pass on the code before this change as well.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The four switches of a mapped query are `[Flags] internal enum QueryOptions : byte` (`None`, `AsyncReads` 1, `SingleResult` 2, `CancelOnEarlyStop` 4, `SequentialAccess` 8), in place of the plain struct: the values are the bits the query's hash code has always mixed in, so the hash part is `(int)_options`, equality is `==`, a copy with a switch set is `_options | QueryOptions.X`, and a switch is read with a mask (`(_options & QueryOptions.X) != 0`, never `HasFlag`, which boxes on the older targets). The core reads them through four private properties of that mask, so the capture tuples of its terminals still pass bools, computed where they are captured, at no cost per row. `Views.NewCore`, `QueryCore` and `FromCommand` pass flag values. No behavior change; the previous tests pass, and a new one pins the flag values.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The doc comments of the members this change touches reuse text instead of repeating it:

- `DbQueryCall.Map<T>()`, `Map(RowMap)`, `Map(Func)`, `AsObjectArrays()` and `AsDictionaryRows()` now hold the text, built from the shared members of `Mapped.docs.xml` (`row-type`, `map-overload`, plus two new ones, `default-bound` for the "default value" exception and `returns-mapped-query` for the one returns line they all repeated). They inherited it from `DbQueryUsing` before.
- The same five members of `DbQueryUsing`, which forward to those, are `<inheritdoc cref=.../>` of them, instead of their own copies of the summary, type parameter, returns and exceptions. (The two types were each other's source: the direction is turned round, as a cycle would resolve nothing.)
- The selector parameter keeps its text that the selector is called from any thread and by overlapping calls, so the shared `map-selector` text, which lacks it, is not used there.

No code change. The members of the internal `QueryOptions` keep one-line docs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Style only, no change to what the code does: every line this change adds or touches (the query core's `With*` one-liners, the `QueryOptions` call sites in `Views` and `MappedDbQuery`, the `ExecuteReader`, `ExecuteAsync` and `RunAsync` calls whose captured switches were renamed, the touched comments and doc comments, and the new tests) is within about 80 columns: an expression body starts on its own line, a long argument list is one argument per line or in natural groups under it, a long ternary is broken at its `?` and `:`, and a comment is wrapped at its phrase breaks. The `<include>` lines of the doc comments stay on one line, as everywhere in the repo.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ions) to set it

`QueryOptions` (`None`, `AsyncReads` 1, `SingleResult` 2, `CancelOnEarlyStop` 4, `SequentialAccess` 8) is a public flags enum in the `Open.Database.Extensions` namespace: how a mapped query executes its command and reads its rows. The summary of each value is inherited from the method of the same name, and the remarks say it applies to the terminals that read rows from a command (a `Map` over a reader or a table you already have has no command to execute, so it has no switches).

`WithOptions(QueryOptions options)` on `MappedQuery<T>`, `MappedQueryWithSkip<T>`, `MappedQueryWithTake<T>`, `MappedCommand<T>` and `MappedDbQuery<T>` returns a query whose switches are exactly `options`. It replaces, it does not combine: any switch not in the value is turned off, including one set earlier in the chain or on the command before `Map` (`SingleResult()`, `AsyncReads()`), so `WithOptions(QueryOptions.None)` clears all four and is the only way to turn a switch off; to add one switch, the named method is the call. Skip and take are kept. A value with a bit that is none of the switches throws `ArgumentOutOfRangeException` (parameter `options`, the value as the actual value) whose message names the valid values, before a default value is looked at. The text that says so is one shared member of `Mapped.docs.xml`, and each named switch method (and the enum) points to `WithOptions` with a `seealso`.

Inside, the query core's four `With*` are one `WithOptions` (set) and one `WithOption` (add one switch); the four public switch methods call `WithOption` and are unchanged for callers. The core's switches are an auto-property (`Options`), read by mask, never with `HasFlag`. `PublicAPI.Unshipped.txt` lists the enum, its values and the five methods; the release notes have a bullet.

Tests: for all 16 combinations, `WithOptions(x)` is the same query (equal, same hash) as the chained calls on a command view, a mapped query, a skipped and a taken query, and a definition (and the definition's query); it replaces earlier switches, and `WithOptions(None)` turns off what a command chose before `Map`, giving the plain query of that command; it keeps skip and take; and a bit that is no switch is refused with the message, on all five types, before a default value is looked at.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence electricessence changed the title MappedQuery dedupe: one options value, one default source, forwarding on the bound query MappedQuery dedupe, and a public QueryOptions with WithOptions to set it Dec 2, 2026
Every type that has the four named switches has `WithOptions(QueryOptions options)`: the typed definition `MappedDbQuery<TParams, T>` had them and not the setter. It forwards to the untyped definition, which checks the value (`ArgumentOutOfRangeException` for a bit that is no switch), and its docs are the untyped one's (inherited), so the four named methods' `seealso` reach it too. `PublicAPI.Unshipped.txt` and the release note list it.

Tests: for all 16 combinations the typed definition's `WithOptions(x)` gives the same query (equal, same hash) as the chained calls, and as the chained calls on the query made from it with `AddParams`; the typed definition refuses a bit that is no switch like the others.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit a5523b1 into master Dec 2, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant